package com.tjl.warehouse.common;

import java.io.Serializable;
import lombok.Data;

/**
 * 通用返回类
 * @author Tang
 */
@Data
public class R<T> implements Serializable {

    private int code;

    private T data;

    private String message;

    private boolean success;

    public R(int code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }
    public R(int code, T data, String message,boolean success) {
        this.code = code;
        this.data = data;
        this.message = message;
        this.success = success;
    }

    public R(int code, T data) {
        this(code, data, "");
    }

    public R(ErrorCode errorCode) {
        this(errorCode.getCode(), null, errorCode.getMessage());
    }

    public R(ErrorCode errorCode, boolean b) {
        this(errorCode.getCode(), null, errorCode.getMessage(),b);
    }


    /**
     * 成功
     * @param data
     * @param <T>
     * @return
     */
    public static <T> R<T> success(T data) {
        return new R<>(ErrorCode.SUCCESS.getCode(), data, "ok",true);
    }
    public static <T> R<T> success(T data,String msg) {
        return new R<>(ErrorCode.SUCCESS.getCode(), data, msg,true);
    }


    public static <T> R<T> error(ErrorCode errorCode) {
        return new R<>(errorCode,false);
    }

    public static <T> R<T> error(T data) {
        return new R<>(ErrorCode.SYSTEM_ERROR.getCode(), data, "失败",false);
    }

    /**
     * 失败
     * @param code
     * @param message
     * @return
     */
    public static <T> R<T> error(int code, String message) {
        return new R<>(code, null, message);
    }

    public static <T> R<T> error(ErrorCode errorCode, String message) {
        return new R<T>(errorCode.getCode(), null, message);
    }
}
